#include<bits/stdc++.h>
using namespace std;

struct node
{
	int a, t;
	bool operator < (const node &x) const
	{
		if(a != x.a)
			return a > x.a;
		else
			return t < x.t;
	}
}ddl[1010];

int vis[1010];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		
		for(int i = 1;i <= n;i++)
			cin>>ddl[i].t;
			
		for(int i = 1;i <= n;i++)
			cin>>ddl[i].a;
		
		sort(ddl+1, ddl+n+1);
		
		memset(vis, 0, sizeof vis);
		
		for(int i = 1;i <= n;i++)
		{
			for(int j = ddl[i].t;j > 0;j--)
			{
				if(!vis[j])
				{
					vis[j] = 1;
					ddl[i].a = 0;
					break;
				}
			}
		}
			
		int ans = 0;
		for(int i = 1;i <= n;i++)
			ans += ddl[i].a;
		cout<<ans<<endl;
	}
	cout<<endl;
	system("pause");
	return 0;
}

